/*
 *  AProject.cpp
 *  libavid
 *
 *  Created by Peter on 25.02.12.
 *  Copyright 2012 __MyCompanyName__. All rights reserved.
 *
 */

#include "AProject.h"
#include "AStream.h"

A_IMPLEMENT_CLASS( 'Iprj', AProject, AObject );

void AProject::readFromStream( AStream& str, const AObjDoc& objd )
{
	UInt8 tagMark, version;
	str.readPod( tagMark );
	str.readPod( version );
	TR_ASSERT( tagMark == 0x02 );
	TR_ASSERT( version == 10 );
	
	Int32 numBins;
	str.readPod( numBins );
	str.readPod( _version );
	
	for( int i = 0; i < 4; ++i )
		str.readPod( _iWindRect[ i ] );
	
	for( int i = 0; i < numBins; ++i ) {
		Bin* bin = new Bin; // TODO: Make exception safe
		
		str.readPod( bin->_fileLocator );
		bin->_wasOpen = str.readBool();
		bin->_wasExternal = str.readBool();
		str.readPod( bin->_unused );
		_bins.push_back( bin );
	}
	
	str.readPod( _iVideoFormat );
	str.readPod( _iSourceFormat );
	str.readPod( _iFilmType );   
	_iFilmEditing = str.readBool();
	
	// ...
}

#define TRACE_FIELD( x ) "\t" #x ": " << x << std::endl

void AProject::dump( std::ostream& str ) const
{
	str << "AProject {" << std::endl;
	
	str << TRACE_FIELD( _version );
	
	str << "\t_iWindRect[ 4 ] {" << std::endl;
	for( int i = 0; i < 4; ++i )
		str << "\t\t" << _iWindRect[ i ] << std::endl;
	
	str << "\t_bins[ " << _bins.size() << " ] {" << std::endl;
	
	BinList::const_iterator it = _bins.begin();
	
	for( ; it != _bins.end(); ++it ) {
		str << "\t\tBin {" << std::endl;
		
		str << "\t\t\t_fileLocator: " << (*it)->_fileLocator << std::endl;
		str << "\t\t\t_wasOpen: " << (*it)->_wasOpen << std::endl;
		str << "\t\t\t_wasExternal: " << (*it)->_wasExternal << std::endl;
		str << "\t\t\t_unused: " << (*it)->_unused << std::endl;
		
		str << "\t\t}" << std::endl;
	}
	str << "\t}" << std::endl;
	
	str << TRACE_FIELD( _iVideoFormat );
	str << TRACE_FIELD( _iSourceFormat );
	str << TRACE_FIELD( _iFilmType );
	str << TRACE_FIELD( _iFilmEditing );
	
	str << "\t..." << std::endl;
	
	str << "}" << std::endl;
}